package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.User;
import com.hcinfos.gsoo.util.SharePreferenceUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class OrderAdressActivity extends Activity implements OnClickListener {

	private List<User> list;
	private LinearLayout address_ll;
	private TextView edit_tv;

	private boolean isUpdate = false;
	private ImageView back_iv;

	private Button add_bt;

	private User user;
	private SharePreferenceUtil util;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orderaddress);
		util = new SharePreferenceUtil(this, "setting");

		add_bt = (Button) findViewById(R.id.add_bt);
		add_bt.setOnClickListener(this);
		edit_tv = (TextView) findViewById(R.id.edit_tv);
		edit_tv.setOnClickListener(this);

		address_ll = (LinearLayout) findViewById(R.id.address_ll);
		back_iv = (ImageView) findViewById(R.id.back_iv);
		back_iv.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		list = new ArrayList<User>();
		user = (User) util.loadObject("user");
		if (user != null && user.getSendAddr1() != null) {
			list.add(user);
			add_bt.setVisibility(View.GONE);
		}
		updateViews(list);
		super.onResume();
	}

	private Intent intent;

	private void updateViews(List<User> list) {

		if (list != null && list.size() > 0) {

			address_ll.removeAllViews();
			for (int i = 0; i < list.size(); i++) {
				View view = getLayoutInflater().from(this).inflate(
						R.layout.address_item, null);
				TextView name_tv = (TextView) view.findViewById(R.id.name_tv);
				TextView phone_tv = (TextView) view.findViewById(R.id.phone_tv);
				TextView address_tv = (TextView) view
						.findViewById(R.id.address_tv);
				ImageView del_iv = (ImageView) view.findViewById(R.id.del_iv);
				ImageView update_iv = (ImageView) view
						.findViewById(R.id.update_iv);
				if (isUpdate) {
					del_iv.setVisibility(View.VISIBLE);
					update_iv.setVisibility(View.VISIBLE);
				} else {
					del_iv.setVisibility(View.GONE);
					update_iv.setVisibility(View.GONE);
				}

				del_iv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						Toast.makeText(OrderAdressActivity.this, "删除", 500)
								.show();
					}
				});
				view.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						intent = new Intent(OrderAdressActivity.this,
								AddressUpdateActivity.class);
						startActivity(intent);
					}
				});
				name_tv.setText(list.get(i).getTaName());
				phone_tv.setText(list.get(i).getTaTelphone());
				address_tv.setText(list.get(i).getSendAddr1());
				address_ll.addView(view);
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.edit_tv:
			if (isUpdate) {
				edit_tv.setText("编辑");
				isUpdate = false;
			} else {
				edit_tv.setText("完成");
				isUpdate = true;
			}
			// updateViews(list);

			break;
		case R.id.back_iv:
			finish();
			break;
		case R.id.add_bt:
			intent = new Intent(this, AddressUpdateActivity.class);
			startActivity(intent);
			break;
		}
	}
}
